<template>
  <div>
    <div>【服务资源占用比】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>
<script setup>
import { ref, onMounted, watch } from "vue";
import * as echarts from "echarts";
const props = defineProps({
  data: {
    type: Object,
    required: true,
  },
});
console.log(props.data);

// 1. 初始化echarts实例
let myChart = null;
const target = ref(null);
onMounted(() => {
  myChart = echarts.init(target.value);
  renderChart();
});
// 2. 构建options配置对象
const renderChart = () => {
  const options = {
    xAxis: {
      type: "category", // x轴类型
      data: props.data.servers.map((item) => item.name),
      axisLabel: {
        color: "#9eb1c8",
      },
    },
    yAxis: {
      type: "value",
      show: false, // 不展示y轴轴线
      max: function (value) {
        return parseInt(value.max * 1.2);
      },
    },
    // 图表绘制位置,对应上下左右
    grid: {
      top: 16,
      left: -26,
      right: 0,
      bottom: 26,
      containLabel: true, // 包含刻度(标签)在内的绘制区域
    },
    // 核心配置
    series: [
      {
        type: "bar",
        data: props.data.servers.map((item) => ({
          name: item.name,
          value: item.value,
        })),
        itemStyle: {
          color: "#479AD3",
          barBorderRadius: 5, // 柱状图圆角
          shadowColor: "rgba(0, 0, 0, 0.3)", // 柱状图阴影颜色
          shadowBlur: 5, // 柱状图阴影模糊程度
        },
        barWidth: 12, // 柱状图宽度
        label: {
          //柱状图字体设置
          show: true,
          position: "top",
          textStyle: {
            color: "#fff",
          },
          formatter: "{c}%", // 字体显示格式
        },
      },
    ],
  };
  myChart.setOption(options);
};

watch(() => props.data, renderChart);
// 3. 通过实例.setOption(option)渲染图表
</script>
<style lang="scss" scoped>
</style> 